/**
 * 
 */
$(function(){
	$(":button .btn-info").click(function() {
		var subid = $(this).attr('id');
		if(subid != undefined && !isNaN(parseInt(subid,10))){
			Url = "/OnlineExam/subedit/" + subid;
			$.ajax({
				url : Url,
				type : "GET",
				dataType : "json",
				success : function(data) {
					if (data.success) {
						$('#name').val(data.subject.name);
						$('#description').val(data.subject.description);
						$('#testCount').val(data.subject.testCount);
						$('#totalTime').val(data.subject.totalTime);
						$('#totalScore').val(data.subject.totalScore);
						$('#myModalLabel').html('修改科目');
						$('#submit').html('修改');
						$("#subjectForm").attr("action", Url);
						$('#addSubject').modal('show');
					}
				}
			});
		}
	})
	
	$('#subadd').click(function() {
		$('#name').val('');
		$('#userName').val('');
		$('#description').val('');
		$('#testCount').val('');
		$('#totalTime').val('');
		$('#totalScore').val('');
		$('#myModalLabel').html('添加科目');
		$('#submit').html('添加');
		$("#userForm").attr("action", "/OnlineExam/addSubject");
	})
	
	//点击管理学生科目按钮时，读取所有学生姓名和学号
	$('.manage-subject-student-btn').click(function(){
		var subId = $(this).attr('id');
		//获取当前选择行的科目
		$.ajax({
			type: 'POST',
			url: "/OnlineExam/getAllStudent",
			data:{
				subjectId:subId
			},
			dataType:"json",
			success:function(data){
				var htmlString = '';
				var selectedUser = data.selectUser;
				//得到所关联的学生 
				for(var i = 0; i < data.allUser.length;i++){
					var user = data.allUser[i];
					var checked = ($.inArray(user.userID,selectedUser) !=-1) ? 'checked="checked"' : ' ';
					htmlString += '<div class="checkbox">';
					htmlString += '	<label>';
					htmlString += ' 	<input name="subjectStudents" class="ace ace-checkbox-2" type="checkbox" userid="'+ user.userID + '" ' + checked + ' />';
					htmlString += ' 	<span class="lbl">' + user.userName + '</span>' ;
					htmlString += ' <label>';
					htmlString += '</div>';
				}
				//把所有学生加入到弹出框中
				$('#subjectStudentContent').html(htmlString);
				$('#subjectId').val(subId);
			}
		});
		$('#subjectStudentModal').modal('show');
	});
	
	//添加学生后的保存按钮
	$('#subjectStudentSaveBtn').on('click',function(){
		var subjectStudents = [];
		//获取所有已近勾上的学生学号
		for(var i =0; i< $('input[name="subjectStudents"]:checked').length;i++){
			subjectStudents[i] = $($('input[name="subjectStudents"]:checked')[i]).attr("userid");
		}
		$.ajax({
			type: 'POST',
			url: '/OnlineExam/saveSubjectStudents',
			data:{
				subjectStudents : subjectStudents, //以数组的形式传到后台
				subjectId: $('#subjectId').val()
			},
			dataType:"json",
			success:function(data){
				//判断与否
				if(data.success) {
					alert('成功');
					$('#subjectStudentModal').modal('hide');
				}else{
					alert('失败');
				}
			}
		})
	})
	
	$('.subject-status').click(function() {
		var CheckSubId = $(this).attr("subid");
		var CheckStutas =$(this).attr("checked");
		if (CheckStutas=="checked"){
			var Stutas = 0;
		}else {
			var Stutas = 1;
		}
		$.ajax({
			type: 'POST',
			url: '/OnlineExam/updateSubjectStatus',
			data:{
				SubId : CheckSubId, 
				status: Stutas
			},
			dataType:"json",
			success:function(data){
				//判断与否
				if(data.success) {
					alert('成功更新状态');
					window.location.reload();
				}else{
					alert('失败---'+ data.errMsg);
				}
			}
		})
		
	})
})